package com.yxs.school.util.md5;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5 {
    /**利用MD5进行加密*/
    public static String encode(String str) {
//        throws NoSuchAlgorithmException, UnsupportedEncodingException
        if (str==null||"".equals(str)){
            return "";
        }
        //确定计算方法
        try {
            MessageDigest md5= MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
            return newstr;
        }catch (NoSuchAlgorithmException n) {
            return "";
        }catch (UnsupportedEncodingException uee) {
            return "";
        }
    }
    public static boolean checkpassword(String newpasswd,String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        if(encode(newpasswd).equals(oldpasswd))
            return true;
        else
            return false;
    }
}
